#include "log_in.h"

int flag_2;
void log_in(char (*name)[128], char *my_pos)
{
    system("clear");
    char in_name[128] = " "; // 用户输入的账号
    char in_pos[128] = " ";  // 用户输入的密码

    int chose;
    int flag_1 = 0;
    int num = 0; // 用于记录输入错误的次数
    /*
    flag_1 = -98  账号正确
    flag_1 = -99  账号错误
    */
    printf("小帅哥快来玩呀!!!\n");
    printf("1.登录\n");
    printf("0.退出\n");
    scanf("%d", &chose);
    while (1)
    {

        if (chose == 1)
        {

            printf("请输入你的账号:");
            scanf("%s", in_name);
            for (size_t i = 0; i < 10; i++)
            {
                if (!strcmp(in_name, name[i]))
                {
                    puts("账号正确!");
                    flag_1 = -98; // 账号正确
                    break;
                }
                if (i == 9) // 这里有bug
                {
                    puts("账号错误!");
                    flag_1 = -99; // 账号错误
                    num++;        // 记录用户输入错误的次数
                }
            }

            if (flag_1 == -98)
            {
                printf("请输入你的密码:");
                scanf("%s", in_pos);
                if (!strcmp(in_pos, my_pos))
                {
                    printf("恭喜这位帅哥登陆成功!\n");
                    Choose_Game();
                    return;
                }
                else
                {
                    puts("密码错误!");
                    num++;
                }
            }

            if (num == 3)
            {
                num = 0; // 错误次数请0
                flag_2 = -1;
                Progress_Bar(0, 0, 5); // 打印进度条
            }
        }
        else if (chose == 0)
        {
            break;
        }
        else
        {
            system("clear");
            printf("输入错误,请重新输入!\n");
            printf("1.登录\n");
            printf("0.退出\n");
            scanf("%d", &chose);
        }
    }
}

void Progress_Bar(int x, int y, float time_length)
{
    float flag = 0;                          // 当前进度时间
    float total_length = 20;                 // 进度条总长度
    float mean = time_length / total_length; // 平均数
    float temp = 0;                          // 和平均数对比
    float percent = 0;                       // 用于显示百分比
    int num = 0;                             // 控制进度条符号的个数

    while (1)
    {

        if (fabs(temp - mean) < EPSILON) // 使用fabs计算浮点数差值的绝对值
        {
            num++;
            temp = 0;
        }
        for (size_t i = 0; i < y; i++)
        {
            putchar('\n');
        }
        for (size_t i = 0; i < x; i++)
        {
            putchar(' ');
        }

        putchar('[');
        for (size_t i = 0; i < num; i++)
        {
            printf(">");
        }
        for (size_t i = 0; i < total_length - num; i++)
        {
            printf(" ");
        }
        putchar(']');
        if (-1 == flag_2)
            percent = ((time_length / 50) * flag) * 200;
        else if (-2 == flag_2 || -3 == flag_2)
            percent = ((time_length / 30) * flag) * 330;
        printf("%.2f%%\n", percent);
        temp += 0.01;
        flag += 0.01;
        if (fabs((flag - 0.01) - time_length) < EPSILON)
        {
            break;
        }
        fflush(stdout);
        usleep(10000);
        system("clear");
        if (-1 == flag_2)
            printf("输入错误超过三次,请等待5秒后重试\n");
        else if (-2 == flag_2)
            printf("欢迎进入系统！\n正在为你加载菜单栏\n");
        else if (-3 == flag_2)
            printf("欢迎下次再玩该游戏!\n请稍后!正在为你加载菜单栏\n");
    }
    system("clear");
}

void Menu_Game()
{

    printf(" ------------------小游戏菜单-------------------\n");
    printf("|                                               |\n");
    printf("|                 1.猜数字小游戏                |\n");
    printf("|                 2.可控金字塔                  |\n");
    printf("|                 3.随机生成学生成绩            |\n");
    printf("|                 4.退出                        |\n");
    printf(" -----------------------------------------------\n");
    printf("请输入你想要选择的游戏编号：\n");
}

void Choose_Game()
{
    int num;
    int flag;
    system("clear");
    flag_2 = -2;
    Progress_Bar(0, 0, 3);
    Menu_Game();
    scanf("%d", &num);
    while (num)
    {
        switch (num)
        {
        case 1:
            Guess();
            break;
        case 2:
            pyramid();
            break;
        case 3:
            Grade();
            break;
        case 4:
            flag = 1;
            break;
        default:
            printf("没有该选项,请重新输入!!!\n");
            continue;
        }
        if (flag == 1)
        {
            system("clear");
            printf("小帅哥欢迎下次来玩~~!!!\n");
            break;
        }
        scanf("%d", &num);
    }
}

void pyramid()
{

    int i, j, k;
    int num;
    system("clear");
    while (1)
    {
        printf("请输入需要生成的金字塔层数:\n");
        printf("(如要退出该游戏请输入-1)\n");
        scanf("%d", &num);
        for (i = 1; i <= num; i++)
        {
            printf("\n");
            for (j = 0; j <= num - 1 - i; j++)
                printf(" ");
            for (k = 0; k < (2 * i - 1); k++)
                printf("*");
        }
        printf("\n");
        if (num == -1)
        {
            flag_2 = -3;
            Progress_Bar(0, 0, 3);
            Menu_Game();
            break;
        }
    }
}

void Guess()
{
    int flag;
    int num;
    int max = 100, min = 0;
    srand(time(NULL));
    int Rand = rand() % 100 + 1;
    system("clear");
    printf("请在0~100之间猜数:");
    while (1)
    {
        scanf("%d", &num);
        if (num == Rand)
        {
            printf("猜对啦!\n");
            printf("欢迎继续游玩!输入-1退出\n");
            while (1)
            {
                scanf("%d", &num);
                if (num == -1)
                {
                    flag_2 = -3;
                    Progress_Bar(0, 0, 3);
                    Menu_Game();
                    flag = 1;
                     
                    break;
                }
                else
                    printf("输入错误,请重新输入!!");
            }
        if (flag == 1)
            break;
        }
        else if (num < Rand)
        {
            if (num > min)
            {
                min = num;
                printf("猜小了,再猜猜,在(%d,%d)之间\n", min, max);
            }
            else if (num <= min)
                printf("请在(%d,%d)之间选择\n", min, max);
        }
        else if (num > Rand)
        {
            if (num < max)
            {
                max = num;
                printf("猜大了,再猜猜,在(%d,%d)之间\n", min, max);
            }
            else if (num >= max)
                printf("请在(%d,%d)之间选择\n", min, max);
        }

    }
}

void Grade()
{
    int key;
    int grade[10];
    int temp;
    srand(time(NULL));
    for (int i = 0; i < sizeof(grade) / sizeof(int); i++)
    {
        grade[i] = rand() % 100 + 1;
    }
    printf("正在进入学生成绩等级查询系统(按1继续)\n");
    scanf("%d", &key);
    if (key == 1)
    {
        for (size_t i = 0; i < sizeof(grade) / sizeof(int) - 1; i++)
        {
            for (size_t j = 0; j < sizeof(grade) / sizeof(int) - i - 1; j++)
            {
                if (grade[j] > grade[j + 1])
                {
                    temp = grade[j];
                    grade[j] = grade[j + 1];
                    grade[j + 1] = temp;
                }
            }
        }
        printf("学生成绩从小到大的排序为:");
        for (size_t i = 0; i < sizeof(grade) / sizeof(int); i++)
            printf("%d\t", grade[i]);
        printf("\n");
        printf("学生等级从小到大的排序为:");
        for (size_t i = 0; i < sizeof(grade) / sizeof(int); i++)
        {
            if (0 < grade[i] && grade[i] <= 60)
            {
                printf("不及格\t");
            }
            else if (60 < grade[i] && grade[i] <= 70)
            {
                printf("D\t");
            }
            else if (70 < grade[i] && grade[i] <= 80)
            {
                printf("C\t");
            }
            else if (80 < grade[i] && grade[i] <= 90)
            {
                printf("B\t");
            }
            else if (90 < grade[i] && grade[i] <= 100)
            {
                printf("A\t");
            }
        }
        printf("\n成绩已排序,请查看后输入-1退出\n");

        while (1)
        {
            scanf("%d", &key);
            if (key == -1)
            {
                flag_2 = -3;
                Progress_Bar(0, 0, 3);
                Menu_Game();
                break;
            }
            else if (key != -1)
                printf("输入错误,请重新输入!!!\n");
        }
    }
}